package views;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import java.awt.Color;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.swing.JList;
import controllers.BoardingCheckController;
import core.models.dataModels.BoardingCheckModel;
import core.models.domainModel.Passagier;



public class BoardingCheckView extends JFrame {
	
	
	public JLabel lblVluchtid_1 = new JLabel("VluchtID:");
	public JLabel lblNaam = new JLabel("Naam :");
	public JLabel lblVluchtnr = new JLabel("VluchtNR :");
	public JLabel lblVoornaam = new JLabel("Voornaam:");
	public JLabel lblVluchtNrValue = new JLabel("New label");
	public JLabel lblGeboortedatum = new JLabel("Geboortedatum :");
	public JLabel lblVluchtIDOutput = new JLabel("New label");
	public JLabel lblNaamOutput = new JLabel("New label");
	public JLabel lblVoornaamOutput = new JLabel("New label");
	public JLabel lblGeboortedatumOutput = new JLabel("New label");
	public JLabel lblActie = new JLabel("New label");
	public JLabel lblPasspoortnummer = new JLabel("Passpoortnummer :");
	public JLabel lblPasspoortnummeroutput = new JLabel("New label");
	
	
	private static final long serialVersionUID = 1L;
	private JTextField txtVluchtID;
	private JTextField txtTicketID;
	private BoardingCheckModel _model = new BoardingCheckModel();
	public JList<Passagier> lstPassagierslijst = new JList<Passagier>();
	public DefaultListModel<Passagier> hetmodel = new DefaultListModel<Passagier>();
	public JButton btnCheckTicket = new JButton("Check Ticket");
	public JButton btnVolgende = new JButton("Volgende");
	public JButton btnCancel = new JButton("Cancel");
	public JLabel lblKlaarVoorVertrek = new JLabel("KLAAR VOOR VERTREK");
	public JLabel lblOngeldigeVluchtnummer = new JLabel("Ongeldige vluchtnummer !");
	
	
	public BoardingCheckView() {
		
		lblOngeldigeVluchtnummer.setForeground(Color.RED);
		lblOngeldigeVluchtnummer.setVisible(false);
				
		// componenten wegwerken
		lblVluchtIDOutput.setVisible(false);
		lblGeboortedatumOutput.setVisible(false);
		lblNaamOutput.setVisible(false);
		lblPasspoortnummeroutput.setVisible(false);
		lblVluchtNrValue.setVisible(false);
		lblVoornaamOutput.setVisible(false);
		lblActie.setVisible(false);
		btnVolgende.setVisible(false);
		lblKlaarVoorVertrek.setVisible(false);
		
		
		
		txtVluchtID = new JTextField();
		txtVluchtID.setColumns(10);
		
		JLabel lblVluchtid = new JLabel("VluchtID :");
		
		JButton btnSetVlucht = new JButton("Set Vlucht");
		btnSetVlucht.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblKlaarVoorVertrek.setVisible(false);
				try
				{
				_model = BoardingCheckController.PassagiersLijstUpdate(_model, Integer.parseInt(txtVluchtID.getText()));
				updateDefaultModel(_model.getPassagierlijst());
				lblOngeldigeVluchtnummer.setVisible(false);
				}
				catch (Exception e)
				{
					lblOngeldigeVluchtnummer.setVisible(true);
				}
			}
		});
		
		txtTicketID = new JTextField();
		txtTicketID.setColumns(10);
		
		
		btnCheckTicket.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				lblVluchtIDOutput.setVisible(false);
				lblGeboortedatumOutput.setVisible(false);
				lblNaamOutput.setVisible(false);
				lblPasspoortnummeroutput.setVisible(false);
				lblVluchtNrValue.setVisible(false);
				lblVoornaamOutput.setVisible(false);
				lblActie.setVisible(false);
				btnVolgende.setVisible(false);
				lblKlaarVoorVertrek.setVisible(false);
				
								
				String ticketID = "z.xml/" + txtTicketID.getText() + ".xml";
				
				
				try
				{
					_model = BoardingCheckController.PassagierGegevensUpdate(_model, ticketID);
					VulGegevensIn(_model.getPassagierGegevens());
				
									
					if (lblVluchtIDOutput.getText().equals(txtVluchtID.getText()))
					{
					lblActie.setText("OK");
					lblActie.setVisible(true);
					btnVolgende.setVisible(true);
					}
					else
					{
						lblActie.setText("VERKEERDE VLUCHT");
						lblActie.setVisible(true);
						btnVolgende.setVisible(false);
					}
					lblVluchtIDOutput.setVisible(true);
					lblGeboortedatumOutput.setVisible(true);
					lblNaamOutput.setVisible(true);
					lblPasspoortnummeroutput.setVisible(true);
					lblVluchtNrValue.setVisible(true);
					lblVoornaamOutput.setVisible(true);
				}catch(FileNotFoundException fnfe){
					lblActie.setText("TACKELEN!!");
					lblActie.setVisible(true);
					btnVolgende.setVisible(false);
				}	


			}
		});
		
		
		lblActie.setForeground(new Color(255, 0, 0));
		lblActie.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		
		btnVolgende.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
								
				for (Passagier passa : _model.getPassagierlijst())
							{
								if (passa.getPaspoortNr().equals(lblPasspoortnummeroutput.getText()))
									{
									_model.removePassagier(passa);
									break;
									}
							}
				updateDefaultModel(_model.getPassagierlijst());
				
				btnVolgende.setVisible(false);
				lblVluchtIDOutput.setVisible(false);
				lblGeboortedatumOutput.setVisible(false);
				lblNaamOutput.setVisible(false);
				lblPasspoortnummeroutput.setVisible(false);
				lblVluchtNrValue.setVisible(false);
				lblVoornaamOutput.setVisible(false);
				lblActie.setVisible(false);
				btnVolgende.setVisible(false);
				
				if (_model.getPassagierlijst().isEmpty())
				{
					lblKlaarVoorVertrek.setVisible(true);
				}

			}
				
			
		});
		
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BoardingCheckController.closeView();
			
			}
		});
		
		JList<Passagier> list = new JList<Passagier>();
		
		JScrollPane scrollPane = new JScrollPane(lstPassagierslijst, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		
		
		
		
		
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(list, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 572, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(11)
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(btnVolgende)
											.addPreferredGap(ComponentPlacement.RELATED))
										.addComponent(lblVoornaam)
										.addComponent(lblNaam)
										.addComponent(lblGeboortedatum)))
								.addComponent(lblPasspoortnummer)
								.addComponent(lblVluchtid_1, Alignment.TRAILING))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(6)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(lblNaamOutput)
										.addComponent(lblVoornaamOutput)
										.addComponent(lblGeboortedatumOutput)
										.addComponent(lblPasspoortnummeroutput)
										.addComponent(lblVluchtIDOutput))
									.addPreferredGap(ComponentPlacement.RELATED, 277, Short.MAX_VALUE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(txtTicketID)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblVluchtid)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addComponent(lblOngeldigeVluchtnummer)
												.addGroup(groupLayout.createSequentialGroup()
													.addComponent(txtVluchtID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(ComponentPlacement.RELATED)
													.addComponent(btnSetVlucht)))))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnCheckTicket))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(85)
									.addComponent(lblKlaarVoorVertrek)
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnCancel)))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblVluchtnr)
									.addGap(18)
									.addComponent(lblVluchtNrValue))
								.addComponent(lblActie))))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblVluchtid)
						.addComponent(txtVluchtID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnSetVlucht))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblOngeldigeVluchtnummer)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtTicketID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCheckTicket))
					.addGap(22)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNaam)
						.addComponent(lblNaamOutput))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblVoornaam)
						.addComponent(lblVoornaamOutput))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGeboortedatum)
						.addComponent(lblGeboortedatumOutput)
						.addComponent(lblActie))
					.addGap(3)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblVluchtid_1)
						.addComponent(lblVluchtIDOutput))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblVluchtNrValue)
								.addComponent(lblVluchtnr))
							.addGap(16))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblPasspoortnummer)
								.addComponent(lblPasspoortnummeroutput))
							.addPreferredGap(ComponentPlacement.UNRELATED)))
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(list, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnCancel)
								.addComponent(btnVolgende))
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblKlaarVoorVertrek)
							.addGap(26))))
		);
		getContentPane().setLayout(groupLayout);
	}
		

	private void updateDefaultModel(ArrayList<Passagier> huidigelijst)
	{
		hetmodel.clear();
		for (Passagier dezepasagier : huidigelijst)
		{
			hetmodel.addElement(dezepasagier);
		}
		lstPassagierslijst.setModel(hetmodel);
	}
	
	private void VulGegevensIn(ArrayList<String> gegevens)
	{
		if (!gegevens.isEmpty())
		{
			lblVoornaamOutput.setText(gegevens.get(0));
			lblNaamOutput.setText(gegevens.get(1));
			lblGeboortedatumOutput.setText(gegevens.get(3));
			lblVluchtNrValue.setText(gegevens.get(5));
			lblVluchtIDOutput.setText(gegevens.get(4));
			lblPasspoortnummeroutput.setText(gegevens.get(2));
		}
		
	}
	
		
}
